package com.rla.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.rla.common.core.page.TableDataInfo;
import com.rla.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.rla.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.rla.domain.bo.BaseDrugTypeBo;
import com.rla.domain.vo.BaseDrugTypeVo;
import com.rla.domain.BaseDrugType;
import com.rla.mapper.BaseDrugTypeMapper;
import com.rla.service.IBaseDrugTypeService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 商品分类Service业务层处理
 *
 * @author rla
 * @date 2025-08-04
 */
@RequiredArgsConstructor
@Service
public class BaseDrugTypeServiceImpl implements IBaseDrugTypeService {

    private final BaseDrugTypeMapper baseMapper;

    /**
     * 查询商品分类
     */
    @Override
    public BaseDrugTypeVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询商品分类列表
     */
    @Override
    public TableDataInfo<BaseDrugTypeVo> queryPageList(BaseDrugTypeBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BaseDrugType> lqw = buildQueryWrapper(bo);
        Page<BaseDrugTypeVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询商品分类列表
     */
    @Override
    public List<BaseDrugTypeVo> queryList(BaseDrugTypeBo bo) {
        LambdaQueryWrapper<BaseDrugType> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BaseDrugType> buildQueryWrapper(BaseDrugTypeBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BaseDrugType> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getParentId() != null, BaseDrugType::getParentId, bo.getParentId());
        lqw.eq(StringUtils.isNotBlank(bo.getAncestors()), BaseDrugType::getAncestors, bo.getAncestors());
        lqw.like(StringUtils.isNotBlank(bo.getName()), BaseDrugType::getName, bo.getName());
        lqw.eq(bo.getOrderNum() != null, BaseDrugType::getOrderNum, bo.getOrderNum());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), BaseDrugType::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增商品分类
     */
    @Override
    public Boolean insertByBo(BaseDrugTypeBo bo) {
        BaseDrugType add = BeanUtil.toBean(bo, BaseDrugType.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改商品分类
     */
    @Override
    public Boolean updateByBo(BaseDrugTypeBo bo) {
        BaseDrugType update = BeanUtil.toBean(bo, BaseDrugType.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BaseDrugType entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除商品分类
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
